﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OA.UserManager
{
    /// <summary>
    /// 角色
    /// </summary>
    public class Role
    {
        #region 属性
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime CreateOn { get; set; }
        public int CreateBy { get; set; }
        public DateTime ModifiedOn { get; set; }
        public int ModifiedBy { get; set; }
        #endregion
        #region 方法
        /// <summary>
        /// 添加一个角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static int Add(Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Users(");
            strSql.Append("Name,Description,CreateOn,CreateBy,ModifiedOn,ModifiedBy)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Description,@CreateOn,@CreateBy,@ModifiedOn,@ModifiedBy)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,20),
					new SqlParameter("@Description", SqlDbType.NVarChar,6),
					new SqlParameter("@CreateOn", SqlDbType.SmallInt,2),
					new SqlParameter("@CreateBy", SqlDbType.NVarChar,255),
					new SqlParameter("@ModifiedOn", SqlDbType.Date,3),
					new SqlParameter("@ModifiedBy", SqlDbType.Bit,1)};
            parameters[0].Value = role.Name;
            parameters[1].Value = role.Description;
            parameters[2].Value = role.CreateOn;
            parameters[3].Value = role.CreateBy;
            parameters[4].Value = role.ModifiedOn;
            parameters[5].Value = role.ModifiedBy;
            object obj = DbHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一个角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool Delete(Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Roles ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = role.Id;

            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 分页查询所有角色
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="size">每页记录数</param>
        /// <returns></returns>
        public static QueryResult<Role> GetAllRoles(int page, int size)
        {
            QueryResult<Role> result = new QueryResult<Role>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "Roles";
            parameters[1].Value = "Id,Name,Description,CreateOn,CreateBy,ModifiedOn,ModifiedBy";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "";
            parameters[9].Value = "Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Role role = new Role();
            while (reader.Read())
            {
                role.Id = reader.GetInt32(0);
                role.Name = reader.GetString(1);
                role.Description = reader.GetString(2);
                role.CreateOn = reader.GetDateTime(3);
                role.CreateBy = reader.GetInt32(4);
                role.ModifiedOn = reader.GetDateTime(5);
                role.ModifiedBy = reader.GetInt32(6);
                result.DataList.Add(role);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }

        /// <summary>
        /// 获取具有某功能权限的角色列表
        /// </summary>
        /// <param name="featureId"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static QueryResult<Role> GetRolesByFeature(int featureId, int page, int size)
        {
            QueryResult<Role> result = new QueryResult<Role>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "Roles INNER JOIN Purview On Purview.RoleId=Roles.Id";
            parameters[1].Value = "Roles.Id,Roles.Name,Roles.Description,Roles.CreateOn,Roles.CreateBy,Roles.ModifiedOn,Roles.ModifiedBy";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "Purview.FeatureId="+featureId;
            parameters[9].Value = "Roles.Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Role role = new Role();
            while (reader.Read())
            {
                role.Id = reader.GetInt16(1);
                role.Name = reader.GetString(2);
                role.Description = reader.GetString(3);
                role.CreateOn = reader.GetDateTime(4);
                role.CreateBy = reader.GetInt32(5);
                role.ModifiedOn = reader.GetDateTime(6);
                role.ModifiedBy = reader.GetInt32(7);
                result.DataList.Add(role);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }
        /// <summary>
        /// 将用户从莫角色中移除
        /// </summary>
        /// <param name="user"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool RemoveUserFromRole(User user, Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UsersInRole");
            strSql.Append("where UserId=@userId and RoleId=@roldId");
            SqlParameter[] parameters = { 
                                            new SqlParameter("@userId",user.Id),
                                            new SqlParameter("@roleId",role.Id)
                                        };
            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 移除某角色的某功能权限
        /// </summary>
        /// <param name="featue">功能权限</param>
        /// <param name="role">角色</param>
        /// <returns></returns>
        public static bool RemoveFeatureFromRole(Feature featue, Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Purview");
            strSql.Append("where RoleId=@RolId and FeatureId=@FeatureId");
            SqlParameter[] parameters = { 
                                            new SqlParameter("@RolId",role.Id),
                                            new SqlParameter("@FeatureId",featue.Id)
                                        };
            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// 给角色授权
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="feature">功能</param>
        /// <param name="createBy">操作人</param>
        /// <returns></returns>
        public static bool AddRoleToFeature(Role role, Feature feature,int createBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [E_OA].[dbo].[Purview] ([RoleId] ,[FeatureId] ,[CreateOn] ,[CreateBy])");
            strSql.Append("VALUES (@RoleId,@FeatureId,@CreateOn,@CreateBy )");
            SqlParameter[] parameters = { 
                                            new SqlParameter("@RolId",role.Id),
                                            new SqlParameter("@FeatureId",feature.Id),
                                            new SqlParameter("@CreateOn",DateTime.Now),
                                            new SqlParameter("@CreateBy",createBy)
                                        };
            int rows = DbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
